/*
 * Copyright (C) 2019 ~ 2022 Uniontech Technology Co., Ltd.
 *
 * Author:     zhangbingyi <zhangbingyi@uniontech.com>
 *
 * Maintainer: zhangbingyi <zhangbingyi@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "peripheralsrevertdialog.h"

#include <QLabel>
#include <QPushButton>

#include <QTimer>

PeripheralsRevertDialog::PeripheralsRevertDialog(DDialog *parent)
    : DDialog(parent)
    , m_elapsedSeconds(15)
    , m_timer(nullptr)
{
    setFixedSize(QSize(450, 200));
    // 弹框样式
    setCloseButtonVisible(true);
    Qt::WindowFlags flags = windowFlags();
    flags |= Qt::WindowStaysOnTopHint;
    setWindowFlags(flags);
//    this->setWindowFlag(Qt::WindowStaysOnTopHint);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    this->setModal(true);

    // 清除布局
    clearContents();
    clearButtons();
    setIcon(QIcon::fromTheme("deepin-defender"));

    // 添加布局
    QLabel *txt = new QLabel(tr("USB ports have been blocked. Please check whether your mouse and keyboard work properly, and decide whether to save current settings. USB port blocking will be turned off when the countdown ends."), this);
    txt->setAlignment(Qt::AlignCenter);
    txt->setWordWrap(true);
    addContent(txt);

    QPushButton *saveBtn = new QPushButton(this);
    saveBtn->setText(tr("Save Current Settings"));
    saveBtn->setAttribute(Qt::WA_NoMousePropagation);
    this->insertButton(0, saveBtn, false);

    QPushButton *revertBtn = new QPushButton(this);
    revertBtn->setText(tr("Revert (%1)").arg(m_elapsedSeconds));
    revertBtn->setAttribute(Qt::WA_NoMousePropagation);
    this->insertButton(1, revertBtn, true);

    // 添加信号槽
    connect(this, SIGNAL(buttonClicked(int, const QString &)), this, SLOT(onButtonClicked(int, const QString &)));

    // 安装定时器
    m_timer = new QTimer();
//    connect(m_timer, &QTimer::timeout, this, [=]() {
//        m_elapsedSeconds--;

//        // 实时变化按钮的时间
//        if (m_elapsedSeconds > 0) {
//            setButtonText(1, tr("Revert (%1)").arg(m_elapsedSeconds));
//        } else {
//            Q_EMIT sendPeripheralsRevert();
//            // 关闭弹框
//            close();
//        }
//    });
    m_timer->start(1000);
}

PeripheralsRevertDialog::~PeripheralsRevertDialog()
{
    Q_EMIT onDialogClose();

    m_timer->stop();
    m_timer->deleteLater();
}

void PeripheralsRevertDialog::onButtonClicked(int index, const QString &text)
{
    Q_UNUSED(text)

    // 停止定时器、关闭弹框
    m_timer->stop();
    if (1 == index) {
        Q_EMIT sendPeripheralsRevert();
    }
    close();
}
